package edu.tgc.web.controller.file;

import java.io.IOException;
import java.util.HashSet;
import java.util.UUID;

import org.abyss.data.specifications.Rule;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.tgc.entity.file.FileDoc;
import edu.tgc.entity.file.FilePath;
import edu.tgc.entity.file.dictionary.FileDocType;
import edu.tgc.service.system.Constant;
import edu.tgc.web.form.file.FileDocForm;

@Controller
@RequestMapping(value = "file/doc")
public class FileDocController extends AbstractFileResourceController<FileDoc, UUID, FileDocForm> {

    @Override
    public Specification<FileDoc> buildSearch(FileDocForm form) {
        HashSet<Rule> rules = new HashSet<>();
        String searchKey = form.getSearchKey();
        if (StringUtils.hasText(searchKey)) {
            rules.add(Rule.or(Rule.property("name").contain(searchKey), Rule.property("sn").contain(searchKey)));
        }
        return Rule.and(rules).<FileDoc> toSpecification();
    }

    @Override
    public FilePath getPath() {
        return new FilePath("d:\\file\\doc");
    }

    public void deleteFile(UUID fileId) throws IOException {
        this.uploadResolverFactory.build(getPath()).delete(fileId.toString(), getPath());
    }

    @Override
    public FileDoc buildCreateEntity(FileDocForm form) {
        FileDoc entity = super.buildCreateEntity(form);
        if (form.getTypeSort() != null) {
            FileDocType type = Constant.getFileDocType(form.getTypeSort());
            if (type != null) {
                entity.setType(type);
            }
        }
        return entity;
    }

    @RequestMapping(value = "flow")
    public void flow() {
    }


}
